package com.st.jms;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.st.jms.handler.MessageHandler;

import org.apache.log4j.Logger;

import java.util.Map;

public class DoneMessage implements MessageHandler {
  private static Logger logger = Logger.getLogger(DoneMessage.class);
  private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
  private int num;
  private StringBuilder stringBuilder;

  public DoneMessage(int num, StringBuilder stringBuilder) {
    this.num = num;
    this.stringBuilder = stringBuilder;
  }

  @Override
  public void doneMessage(byte[] message) {
    try {
      Map data = gson.fromJson(new String(message), Map.class);
      Long delay = System.currentTimeMillis() - Long.parseLong((String) data.get("time"));
      System.out.println("num:"+num+",delay:"+delay+"message:"+ new String(message));
      Integer index = new Integer((String) data.get("text"));
      synchronized (this) {
        stringBuilder.setCharAt(index, '1');
//        System.out.println("stringBuilder:"+stringBuilder);
      }
    } catch (Exception e) {
      logger.info(e.getMessage());
    }
  }
}
